package com.bwc.aidemo.amap

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.amap.api.maps.AMap
import com.amap.api.maps.MapView
import com.amap.api.maps.model.LatLng
import com.amap.api.maps.model.Polyline
import com.bwc.aidemo.R
import com.mojawa.mj_amap.MJMApViewLifecycle
import com.mojawa.mj_amap.util.MJMapViewUtil
import com.bwc.aidemo.arouter.RoutePath
import com.bwc.aidemo.databinding.ActivityRunEndmapBinding


@Route(path = RoutePath.endmap)
class RunEndmapActivity : AppCompatActivity() {
    private lateinit var databinding : ActivityRunEndmapBinding
    private  var aMap : AMap? = null
    private lateinit var mapView: MapView
    private lateinit var mMJMApViewLifecycle : MJMApViewLifecycle
    private var points: ArrayList<LatLng> = ArrayList()
    private lateinit var polyline: Polyline
    @Autowired
    @JvmField
    var endMapLang: ArrayList<AMapLatLng>? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        databinding = DataBindingUtil.setContentView(this, R.layout.activity_run_endmap)
        ARouter.getInstance().inject(this)
        mMJMApViewLifecycle = MJMApViewLifecycle()
        mapView = databinding.appRunendMap
        mMJMApViewLifecycle.onCreate(savedInstanceState, mapView)
        if (aMap == null) {
            aMap = mapView!!.map
        }
        endMapLang?.let {
            // 将 AMapLatLng 转换为 AMap 的 LatLng
            points = ArrayList(it.map { amapLatLng ->
                LatLng(amapLatLng.latitude, amapLatLng.longitude)
            })

        }
        databinding.xingzouguiji.setOnClickListener {
            MJMapViewUtil.showLines(aMap,points)
        }


        databinding.guijihuifang.setOnClickListener {

            aMap?.let { it1 -> MJMapViewUtil.drawAnimatedPolyline(it1,points) }
//            val polylineOptions = PolylineOptions()
//                .width(20f)
//                .color(Color.BLUE)
//
//            polyline = aMap?.addPolyline(polylineOptions)!!
//            // 启动逐帧动画
//            val lineAnimator = LineDrawingAnimator(5000L) { fraction ->
//                // 动画的进度在 fraction 参数中传入
//                val pointsToShow = (points.size * fraction).toInt()
//                val subList = points.subList(0, pointsToShow.coerceAtMost(points.size))
//
//                // 更新Polyline的点，确保逐步添加
//                polyline.points = subList
//            }
//
//            // 启动动画
//            lineAnimator.start()
        }

        databinding.qingkong.setOnClickListener {
            aMap?.clear()
        }


    }
}